﻿Imports System.Data.OleDb
Public Class details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        img_like.Visible = False
        Dim oledbconn = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        Dim sql = "select * from userlikes where reader = @a1 and carid= @a2"
        Dim oledbcmd As New OleDbCommand(sql, oledbconn)

        oledbcmd.Parameters.AddWithValue("@a1", User.Identity.Name)
        oledbcmd.Parameters.AddWithValue("@a2", Request.Params("ID"))
        oledbconn.Open()

        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()

        If dr.HasRows Then
            'display the like-smiley image  and hide the button
            img_like.Visible = True
            btn_like.Visible = False
        Else
            'hide the image and show the button
            img_like.Visible = False
            btn_like.Visible = True
        End If


    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim sql As String = "INSERT INTO comments (author,title,commentbody,cars_fk_id) VALUES(@a1,@a2,@a3,@a4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@a1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@a2", txtbox_title.Text)
        cmd.Parameters.AddWithValue("@a3", txtbox_commentbody.Text)
        cmd.Parameters.AddWithValue("@a4", Request.QueryString("ID"))
        conn.Open()

        If (cmd.ExecuteNonQuery()) Then
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            MsgBox("comment saved")
            Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))

        Else
            MsgBox("Sorry, your comment wasnt saved. chek and be sure everything is okay!!..")
            Return
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim sql As String = "INSERT INTO cartags (car_fk_id,tag_fk_id) VALUES(@a1,@a2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        conn.Open()

        For Each cb As ListItem In cb_tags.Items
            If cb.Selected = True Then
                'add this to the database
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@a1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@a2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

    End Sub

    Protected Sub btn_newtag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_newtag.Click

        Dim sql As String = "INSERT INTO tags (tagname) VALUES (@a1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@a1", tb_newtag.Text)
        cmd.ExecuteNonQuery()

        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID from tags ORDER BY ID DESC", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestid = dr(0)

        Dim associate As String = "INSERT INTO cartags (car_fk_id,tag_fk_id) VALUES (@a1,@a2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(associate, conn)
        cmd3.Parameters.AddWithValue("@a1", Request.QueryString("ID"))
        cmd3.Parameters.AddWithValue("@a2", highestid)
        cmd3.ExecuteNonQuery()

    End Sub

   
    Protected Sub btn_rate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_rate.Click

        Dim oledbconn = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        oledbconn.Open()
        Dim dpr = "delete from userratingcars where rater=@a1 and carid=@a2"
        Dim oledbcmd2 = New OleDbCommand(dpr, oledbconn)
        oledbcmd2.Parameters.AddWithValue("@a1", User.Identity.Name)
        oledbcmd2.Parameters.AddWithValue("@a2", Request.Params("ID"))
        oledbcmd2.ExecuteNonQuery()




        Dim sql = "insert into userratingcars(rating, rater, carid) values(@a1,@a2,@a3)"
        Dim oledbcmd = New OleDbCommand(sql, oledbconn)

        oledbcmd.Parameters.AddWithValue("@a1", Rating1.CurrentRating)
        oledbcmd.Parameters.AddWithValue("@a2", User.Identity.Name)
        oledbcmd.Parameters.AddWithValue("@a3", Request.Params("ID"))
        oledbcmd.ExecuteNonQuery()
        oledbconn.Close()
        oledbcmd.Dispose()
        oledbconn.Dispose()

    End Sub

    Protected Sub btn_like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_like.Click
        img_like.Visible = True
        btn_like.Visible = False

        Dim oledbconn = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        Dim sql = "insert into userlikes(carid,reader) values(@a1,@a2)"
        Dim oledbcmd As New OleDbCommand(sql, oledbconn)

        oledbconn.Open()
        oledbcmd.Parameters.AddWithValue("@a1", Request.Params("ID"))
        oledbcmd.Parameters.AddWithValue("@a2", User.Identity.Name)
        oledbcmd.ExecuteNonQuery()
        oledbconn.Close()
        oledbcmd.Dispose()
        oledbconn.Dispose()


    End Sub
End Class